﻿using System;
using System.Collections.Generic;
using System.IO;
using Windows.UI.Xaml.Media.Imaging;
using System.Linq;
using System.Windows.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Input;
using Windows.UI.Core;
using System.Threading;

namespace RoboRun
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        float width; //Device width
        float height; //Device height

        public MainPage()
        {
            this.InitializeComponent();
          
            obtainScreenDimensions();
            setUpInvisibleButton();
            startLoadingAssets();
        }

   

        private void obtainScreenDimensions()
        {
            width = ScreenDimensions.obtainScreenWidth();
            height = ScreenDimensions.obtainScreenHeight();
        }

        private void setUpInvisibleButton()
        {
            button.Opacity = 0;
            button.Width = width;
            button.Height = height;
        }

        bool isTouchEnabled; //Does the device have touch input?

        private async void startLoadingAssets()
        {
            logoFadeIn();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            if (checkKeyboardIsPresent() == true) //implies Touch Input FOR TABLET TESTING IN SIMULATOR, CHANGE TO TRUE
            {
                isTouchEnabled = true;
                button.IsHitTestVisible = true;
                prompt.Source = new BitmapImage(new Uri("ms-appx:///Assets/SplashScreen/SplashScreenPromptTouch.png", UriKind.Absolute
                    ));
                flashingPrompt();
               
            }
            else //Implies Keyboard Input
            {
                isTouchEnabled = false;
                button.IsRightTapEnabled = false;
                button.IsTapEnabled = false;
                button.IsHitTestVisible = false;
                flashingPrompt();
            }
        }

        private async void logoFadeIn()
        {
            double i = 0;
            while (i < 1) //Continually increases opacity of logo
            {
                logo.Opacity = i;
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(3));
                i += 0.01;
            } 
        }

        private Boolean checkKeyboardIsPresent()
        {
            KeyboardCapabilities kc = new Windows.Devices.Input.KeyboardCapabilities();
            if (kc.KeyboardPresent >= 1)
            {
                return true;
            }
            return false;
        }

        bool b = true;
        private async void flashingPrompt()
        {
            double j = 0;
            while(j < 1 && b == true)
            {
                prompt.Opacity = j;
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                j  += 0.01;
            }
            while (j > 0 && b == true)
            {
                prompt.Opacity = j;
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                j -= 0.01;
            }
            if (b == true)
            {
                flashingPrompt();
            }

        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.Loaded += delegate { this.Focus(FocusState.Programmatic); };
        }

        private void buttonClick(object sender, RoutedEventArgs e)
        {
            b = false;
            if (isTouchEnabled == true)
            {
                launchMainMenu();
            }

        }

        private async void launchMainMenu()
        {
            button.IsEnabled = false;
            promptRapidFlash();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(2000));
            for (int l = 0; l <= width; l+=30)
            {
                if (logo.Width < 100)
                {
                    logo.Width = 0;
                    break;
                }
                else
                {
                    logo.Width = width - l;
                }
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
            }
            c = false;
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(5));
            for (int m = 0; m <= width; m += 30)
            {
                if (prompt.Width < 100)
                {
                    prompt.Width = 0;
                    break;
                }
                else
                {
                    prompt.Width = width - m;
                }
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
            }
            this.Frame.Navigate(typeof(MainMenu));


        }

        bool c = true;
        private async void promptRapidFlash()
        {
            
            double k = 0;
            while (k < 1 && c == true)
            {
                prompt.Opacity = k;
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                k += 0.1;
            }
            while (k > 0 && c == true)
            {
                prompt.Opacity = k;
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                k -= 0.1;
            }
            if (c == true)
            {
                promptRapidFlash();
            }
        }

        bool keyPressed;

        private void button_KeyUp_1(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter && keyPressed == true)
            {
                b = false;
                keyPressed = false;
                launchMainMenu();
                
            }
        }

        private void button_KeyDown_1(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                keyPressed = true;
            }
            button_KeyUp_1(sender, e);
        }
      

       
    }
}
